package org.ydsw.data.manage.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * <p>Title: utils </p>
 * <p>Description：
 * 时间处理工具类
 * </p>
 *
 * @author admin
 * @version 1.0.0
 */
public class DateUtil {

    /**
     * 日期格式：yyyy-MM-dd HH:mm:ss
     */
    public static final String DATE_PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期格式：yyyy-MM-dd
     */
    public static final String DATE_PATTERN_DAY = "yyyy-MM-dd";

    /**
     * 日期格式yyyyMMdd
     */
    public static final String DATE_PATTERN = "yyyyMMdd";

    /**
     * 获取当前日期
     *
     * @return String
     */
    public static String getCurrentDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN_DEFAULT, Locale.getDefault());
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 0);
        return dateFormat.format(cal.getTime());
    }

    /**
     * 获取当前日期
     *
     * @return Date
     */
    public static Date getCurrentTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 0);
        return cal.getTime();
    }

    /**
     * 获取传入时间的前几分钟时间
     *
     * @return String
     */
    public static String getMinutesBefore(Date date, int min) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN_DEFAULT, Locale.getDefault());
        return dateFormat.format(date.getTime() - min * 60 * 1000L);
    }

    /**
     * 字符串转日期格式
     *
     * @return Date
     */
    public static Date getDateByStr(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN_DEFAULT, Locale.getDefault());
        Date parse = null;
        try {
            parse = dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 日期格式转字符串
     *
     * @return String
     */
    public static String getStrByDate(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN_DEFAULT, Locale.getDefault());
        return dateFormat.format(date);
    }

    /**
     * 获取当前日期的00:00:00时间
     *
     * @return String
     */
    public static String getTodayStartTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN_DEFAULT, Locale.getDefault());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * @Description: 获取当天日期时间00:00:00的前8小时时间
     * @return: java.lang.String
     */
    public static String subtractEight() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN_DEFAULT, Locale.getDefault());
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 获取当前日期的23:59:59时间
     *
     * @return String
     */
    public static String getTodayEndTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN_DEFAULT, Locale.getDefault());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * @param date 时间
     * @Description: 获取传入时间前8小时时间
     * @return: java.util.Date
     */
    public static Date subtractEightTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, -8);
        return cal.getTime();
    }

    /**
     * @param date 时间
     * @Description: 获取传入时间8小时后的时间
     * @return: java.util.Date
     */
    public static Date addEightTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, +8);
        return cal.getTime();
    }

    /**
     * @param str 时间字符串
     * @Description: 获取字符串传入时间8小时后的时间
     * @return: java.lang.String
     */
    public static String addEightHourByStr(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_DEFAULT, Locale.getDefault());
        String format = null;
        try {
            Date date = sdf.parse(str);
            format = sdf.format(new Date(date.getTime() + 8 * 60 * 60 * 1000));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format;
    }


    /**
     * @param str 时间字符串
     * @Description: 获取字符串传入时间8小时前的时间
     * @return: java.lang.String
     */
    public static String subtractEightHourByStr(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_DEFAULT, Locale.getDefault());
        String format = null;
        try {
            Date date = sdf.parse(str);
            format = sdf.format(new Date(date.getTime() - 8 * 60 * 60 * 1000));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format;
    }

    /**
     * @param date 时间
     * @param min 倒退几分钟
     * @Description: 根据传入的时间和数字，计算出需要获取的时间字符串
     * @return: java.lang.String
     */
    public static String runStartDate(Date date, int min) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_DEFAULT, Locale.getDefault());
        return sdf.format(date.getTime() - 8 * 60 * 60 * 1000L - min * 60 * 1000L);
    }

    /**
     * 获取当前日期,从0小时0分钟0秒开始
     *
     * @return 2021-02-05 13:44
     * @param pattern 格式内容
     */
    public static String getDateByString(String pattern) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());
        return sdf.format(cal.getTime());
    }

    /**
     * 获取当前时间，从23小时59分钟59秒结束
     *
     * @param pattern 时间格式
     * @return 2021-02-05 23:59:59
     */
    public static String getDateByEnd(String pattern){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());
        return sdf.format(cal.getTime());
    }

    /**
     * @param str 时间字符串
     * @Description: 获取前一分钟时间
     * @return: java.lang.String
     */
    public static String addOneMin(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_DEFAULT, Locale.getDefault());
        String format = null;
        try {
            Date date = sdf.parse(str);
            format = sdf.format(new Date(date.getTime() + 60 * 1000));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format;
    }

    /**
     * @Description:
     * @return: java.lang.String
     */
    public static String getCurrentDay() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN_DAY, Locale.getDefault());
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 0);
        return dateFormat.format(cal.getTime());
    }

    /**
     * 根据日期获取当天是周几
     * @param datetime 日期
     * @return 周几
     */
    public static int dateToWeek(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        Calendar cal = Calendar.getInstance();
        Date date;
        try {
            date = sdf.parse(datetime);
            cal.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return cal.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * 获取当前日期所在的年份
     * @return 返回当前年份，如2021
     */
    public static String getYearByDay(){
        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        return String.valueOf(year);
    }

    /**
     * 获取当前时间指定天数的日期
     * @param diff
     * @return
     */
    public static String getCurrentDayByDiff(int diff){
        SimpleDateFormat dateFormat= new SimpleDateFormat(DATE_PATTERN_DAY);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, diff);
        String currentDay = dateFormat.format(cal.getTime());
        return currentDay;
    }


    /**
     * 获取当前时间指定天数的日期
     * @param diff
     * @return
     */
    public static String getCurrentDayByDiffOtherFormat(int diff){
        SimpleDateFormat dateFormat= new SimpleDateFormat(DATE_PATTERN);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, diff);
        String currentDay = dateFormat.format(cal.getTime());
        return currentDay;
    }
    public static void main(String[] args) {
        System.out.println(getCurrentDayByDiffOtherFormat(-10));
    }

}
